<?php

class PurchaseOrderProduct extends Eloquent {
	 protected $table = 'purchase_order_product';

	/**
	 * Condition: Unarchived Items.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @param $query
	 * @return mixed
	 */
	public function scopeActive( $query ) {
		return $query->where($this->table . '.archived', 0);
	}

	/**
	 * Retrieves Purchase Order Products under Pending and Approved already.
	 * @author AngelaMisa 02/28/2015 10:00PM
	 * @param $query, $item_ids
	 * @return mixed
	 */
	public function scopePoItems( $query, $item_ids = array() ) {
		$query->where('archived', 0);

		if ( !empty($item_ids) )
			$query->whereIn( 'ORDER_PRODUCT_id', $item_ids );

		return $query->whereHas('purchaseOrder', function ( $po ) {
			$po->whereIn( 'status', array(Constant::PO_PENDING, Constant::PO_APPROVED) );
		});
	}

	/**
	 * Relates to Purchase Order.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @return mixed
	 */
	public function purchaseOrder() {
		return $this->belongsTo('PurchaseOrder', 'PURCHASE_ORDER_id');
	}

	/**
	 * Relates to Order Product.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @return mixed
	 */
	public function orderProduct() {
		return $this->belongsTo('OrderProduct', 'ORDER_PRODUCT_id');
	}
}